﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Wuqi.Webdiyer;

/// <summary>
/// WebPager 的摘要说明
/// </summary>
public class WebPager
{
    #region 私有变量
    private SqlConnection conn;
    private SqlCommand cmd;
    #endregion

    #region 构造函数
    public WebPager()
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dataBaseConnection"].ConnectionString);
    }
    
    #endregion

    /// <summary>
    /// 页面载入
    /// </summary>
    /// <param name="pager">分页控件对象</param>
    /// <param name="dg">GridView对象</param>
    /// <param name="pageParm">分页数据参数对象</param>
    /// <param name="isVs">是否是执行查询分页</param>
    public void PageLoadBinding(ref Wuqi.Webdiyer.AspNetPager pager, ref System.Web.UI.WebControls.GridView dg,PageBean pageParm, bool isVs)
    {
        cmd = new SqlCommand("news_GetPagedNews", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@TableList", pageParm.TableList);
        cmd.Parameters.Add("@TableName", pageParm.TableName);
        cmd.Parameters.Add("@SelectOrderId", pageParm.SelectOrderID);
        cmd.Parameters.Add("@SelectOrder", pageParm.SelectOrder);
        cmd.Parameters.Add("@pageindex", 1);
        cmd.Parameters.Add("@pagesize", pager.PageSize);
        cmd.Parameters.Add("@docount", true);
        conn.Open();
        pager.RecordCount = (int)cmd.ExecuteScalar();
        conn.Close();
        BindData(ref pager, ref dg, pageParm, isVs);
    }

    /// <summary>
    /// 绑定数据源
    /// </summary>
    /// <param name="pager">分页控件对象</param>
    /// <param name="dg">dataGridView对象</param>
    /// <param name="pageParm">分页数据参数对象</param>
    /// <param name="isVs">是否是执行查询分页</param>
    public void BindData(ref Wuqi.Webdiyer.AspNetPager pager, ref System.Web.UI.WebControls.GridView dg, PageBean pageParm, bool isVs)
    {
        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        if (isVs)
        {
            cmd.CommandText = "news_GetSearchResult";
            cmd.Parameters.Add("@TableList", pageParm.TableList);
            cmd.Parameters.Add("@TableName", pageParm.TableName);
            cmd.Parameters.Add("@SelectOrderId", pageParm.SelectOrderID);
            cmd.Parameters.Add("@SelectOrder", pageParm.SelectOrder);
            cmd.Parameters.Add("@SelectWhere", pageParm.SelectWhere);
            cmd.Parameters.Add("@pageindex", pager.CurrentPageIndex);
            cmd.Parameters.Add("@pagesize", pager.PageSize);
            cmd.Parameters.Add("@docount", false);
        }
        else
        {
            cmd.CommandText = "news_GetPagedNews";
            cmd.Parameters.Add("@TableList", pageParm.TableList);
            cmd.Parameters.Add("@TableName", pageParm.TableName);
            cmd.Parameters.Add("@SelectOrderId", pageParm.SelectOrderID);
            cmd.Parameters.Add("@SelectOrder", pageParm.SelectOrder);
            cmd.Parameters.Add("@pageindex", pager.CurrentPageIndex);
            cmd.Parameters.Add("@pagesize", pager.PageSize);
            cmd.Parameters.Add("@docount", false);
        }
        conn.Open();
        dg.DataSource = cmd.ExecuteReader();
        dg.DataBind();
        conn.Close();
        AddCustomText(ref pager);
    }

    /// <summary>
    /// 执行查询分页
    /// </summary>
    /// <param name="pager">分页控件对象</param>
    /// <param name="dg">GridView对象</param>
    /// <param name="intMark">数据行记数器，以确定是否存在记录（大于0时）</param>
    /// <param name="pageParm">分页数据对象</param>
    /// <param name="isVs">是否是执行查询分页</param>
    public void SearchNews(ref Wuqi.Webdiyer.AspNetPager pager, ref System.Web.UI.WebControls.GridView dg,ref int intMark, PageBean pageParm, bool isVs)
    {

        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "news_GetSearchResult";

        cmd.Parameters.Add("@TableList", pageParm.TableList);
        cmd.Parameters.Add("@TableName", pageParm.TableName);
        cmd.Parameters.Add("@SelectOrderId", pageParm.SelectOrderID);
        cmd.Parameters.Add("@SelectOrder", pageParm.SelectOrder);
        cmd.Parameters.Add("@SelectWhere", pageParm.SelectWhere);
        cmd.Parameters.Add("@pageindex", 1);
        cmd.Parameters.Add("@pagesize", pager.PageSize);
        cmd.Parameters.Add("@docount", true);
        conn.Open();
        intMark = (int)cmd.ExecuteScalar();
        pager.RecordCount = intMark;
        conn.Close();
        BindData(ref pager, ref dg, pageParm, isVs);

    }


    #region 私有方法
    private void AddCustomText(ref Wuqi.Webdiyer.AspNetPager pager)
    {
        //动态设置用户自定义文本内容
        pager.CustomInfoText = "记录总数：<font color=\"blue\"><b>" + pager.RecordCount.ToString() + "</b></font>";
        pager.CustomInfoText += " 总页数：<font color=\"blue\"><b>" + pager.PageCount.ToString() + "</b></font>";
        pager.CustomInfoText += " 当前页：<font color=\"red\"><b>" + pager.CurrentPageIndex.ToString() + "</b></font>";
    }

    #endregion


}
